package com.llsly.library.controller;

import com.llsly.library.model.Book;
import com.llsly.library.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/library")
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    @PostMapping("/add")
    public Book addBook(@RequestBody Book book) {
        return libraryService.addBook(book);
    }

    @DeleteMapping("/remove/{id}")
    public ResponseEntity<Void> removeBook(@PathVariable Long id) {
        libraryService.removeBook(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/count")
    public long getBookCount() {
        return libraryService.getBookCount();
    }

    @GetMapping("/has/{title}")
    public boolean hasBook(@PathVariable String title) {
        return libraryService.hasBook(title);
    }

    @PutMapping("/update/{id}")
    public Book updateBook(@PathVariable Long id, @RequestBody Book updatedBook) {
        return libraryService.updateBook(id, updatedBook);
    }
}